Implementing the sequence algorithm